package com.shujia.data

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import scala.io.Source

object CarsToKafka {
  def main(args: Array[String]): Unit = {

    val properties = new Properties

    //1、指定kafkabroker地址
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")

    //2、指定kv的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")


    //创建kafka的生产者
    val producer = new KafkaProducer[String, String](properties)



    //读取数据
    Source
      .fromFile("C:\\Users\\qx\\Documents\\WPS Cloud Files\\22672127\\团队文档\\马鞍山\\项目\\智慧交通\\cars.json")
      .getLines()
      .foreach(car => {

        val record = new ProducerRecord[String, String]("cars", car)

        producer.send(record)

        producer.flush()

        Thread.sleep(40)
      })


    producer.close()
  }

}
